<?php

/**
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$cron_lang = ROOT_PATH . 'languages/' .$GLOBALS['_CFG']['lang']. '/cron/order_info.php';
if (file_exists($cron_lang))
{
    global $_LANG;

    include_once($cron_lang);
}

/* 模块的基本信息 */
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = isset($modules) ? count($modules) : 0;

    /* 代码 */
    $modules[$i]['code']    = basename(__FILE__, '.php');

    /* 描述对应的语言项 */
    $modules[$i]['desc']    = 'zz_order_desc';

    /* 作者 */
    $modules[$i]['author']  = 'xie';

    /* 网址 */
    $modules[$i]['website'] = '';

    /* 版本号 */
    $modules[$i]['version'] = '1.0.0';

    /* 配置信息 */
    $modules[$i]['config']  = '';

    return;
}

$num = 50;

// $where =  "WHERE oi.order_status=".OS_CONFIRMED;
// $where .= " AND oi.pay_status=".PS_PAYED;
// $where .= " AND oi.push_status=".PUS_NOTPUSH;
// $where .= " AND oi.shipping_status=".SS_UNSHIPPED;

// $sql = "SELECT COUNT(*) FROM ".
// 		$ecs->table('order_info')." AS oi ".
// 		$where;

// $total = $db->getOne($sql);

// while ($total>0)
// {
// 	push($limit);
// 	$total -= $limit;
// }

$where =  "WHERE oi.order_status=".OS_CONFIRMED;
$where .= " AND oi.pay_status=".PS_PAYED;
$where .= " AND oi.zz_status=".PUS_PUSHED;
$where .= " AND oi.shipping_status=".SS_UNSHIPPED;
$where .= " AND og.suppliers_id=2"; //卓志管理的仓库

// $limit = " LIMIT ".$num;

$orderBy = " ORDER BY oi.add_time DESC ";
$groupBy = " GROUP BY oi.order_sn ";

$sql = "SELECT oi.* FROM ".
		$GLOBALS['ecs']->table('order_info')." AS oi ".
		" LEFT JOIN ".$GLOBALS['ecs']->table('order_goods')." AS og ON og.order_id=oi.order_id ".
		$where.$groupBy.$orderBy;

$orders = $GLOBALS['db']->getAll($sql);
// print_r($orders);

if (empty($orders)) return;
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/zhuozhi.php');
$temp = array();
foreach ($orders as $key=>$row)
{
	$temp[$key]['orderId'] = $row['pp_sn'];
	$temp[$key]['cbepcomcode'] = '1000000202';
// 	print_r($temp);
	$result = get_order_info($temp[$key]);
	
	if ($result['status']!=1)
	{
		$order_sn = get_order_id_by_pp($row['pp_sn']);
		push_log($order_sn, $result['status'], $result['notes'], '查询订单进度','','',2);
		$fp2=@fopen(ROOT_PATH.'log/zz_query.txt','a');
		fwrite($fp2, '****************************************'.PHP_EOL);
		fwrite($fp2,date('Y-m-d H:i:s').':'.json_encode($result).PHP_EOL);
		fwrite($fp2, '****************************************'.PHP_EOL);
		fclose($fp2);
	}
	else 
	{
		$latest = array_pop($result['statusList']);
		switch ($latest['orderStatus'])
		{
			case 10:
				$status=4;
			break;
			case 70:
				$status=5;
			break;
			case 80:
				$status=6;
			break;
			case 90:
				$status=7;
				$sql = "SELECT shipping_status FROM ".$GLOBALS['ecs']->table('order_info')." WHERE pp_sn=$row[pp_sn] ";
				$ship_status = $GLOBALS['db']->getOne($sql);
				if ($ship_status==SS_SHIPPED_PART)
				{
					$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info')." SET shipping_status=".SS_SHIPPED." WHERE pp_sn=$row[pp_sn] ";
					$GLOBALS['db']->query($sql);
				}
				elseif ($ship_status==SS_UNSHIPPED)
				{
					$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info')." SET shipping_status=".SS_SHIPPED_PART." WHERE pp_sn=$row[pp_sn] ";
					$GLOBALS['db']->query($sql);
				}
			break;
			case 95:
				$status=8;
			break;
			default:
				$status=0;
			break;
		}
		if ($status>0)
		{
			$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info')." SET zz_status=".$status." WHERE pp_sn=$row[pp_sn] ";
			$GLOBALS['db']->query($sql);
		}
	}
}










?>